import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    host: '0.0.0.0', // 允许外部访问
    port: 5174,      // 确保端口一致，sunny-nork对应的时5174
    proxy: {
      '/api/': {
        target: 'http://localhost:8080', // 代理到后端服务
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''), // 去掉开头的 /api
        timeout: 20000, // 设置超时时间
        secure: false, // 不校验证书
        ws: true, // proxy websockets
      }
    },
    hmr: {
      clientPort: 5174, // 确保端口一致
    },
    // 跳过检查
    disableHostCheck: true,
  },
  optimizeDeps: {
    // 开发时 解决这些commonjs包转成esm包
    include: [
      "@jiaminghi/c-render",
      "@jiaminghi/c-render/lib/plugin/util",
      "@jiaminghi/charts/lib/util/index",
      "@jiaminghi/charts/lib/util",
      "@jiaminghi/charts/lib/extend/index",
      "@jiaminghi/charts",
      "@jiaminghi/color",
    ],
  }
})
